//
//  RFeedbackViewController.m
//  iPlan
//
//  Created by 侯伟 on 2021/9/18.
//

#import "RFeedbackViewController.h"
#import "XTextField.h"
#import "RSVerButton.h"
#import "RFeedbackRequest.h"

@interface RFeedbackViewController ()<UITextFieldDelegate,UITextViewDelegate>

/// 下方视图承载
@property(nonatomic,strong)MyLinearLayout *contentLayout;

/// 选项
@property(nonatomic,strong)MyFlowLayout *flowLayout;

/// 反馈内容
@property(nonatomic,strong)UITextView *textView;

/// 占位文本
@property (nonatomic,weak)UILabel* placeLb;

/// 字数
@property (nonatomic,weak)UILabel* numLb;

/// 联系方式
@property(nonatomic,strong)XTextField *contactTextField;

@end

@implementation RFeedbackViewController

- (void)loadView{
    [super loadView];
   
    self.view.backgroundColor = UIColor.whiteColor;
    // 根视图
    MyLinearLayout *rootLayout = [MyLinearLayout linearLayoutWithOrientation:(MyOrientation_Vert)];
    rootLayout.myMargin = 0;
    [self.view addSubview:rootLayout];
    

    //下方滚动先行视图
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.myMargin = 0;
    scrollView.weight = 1.0;
    [rootLayout addSubview:scrollView];
    
    MyLinearLayout *contentLayout = [MyLinearLayout linearLayoutWithOrientation:(MyOrientation_Vert)];
    contentLayout.myTop = 0;
    contentLayout.myHorzMargin = 0;
    contentLayout.heightSize.lBound(scrollView.heightSize, 10, 1);
    [scrollView addSubview:contentLayout];
    _contentLayout = contentLayout;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupNavBar];
    
    [self initSubViews];
    
    NSArray* tags = @[@{@"title":@"闪退",@"icon_n":@"feedback_st_n",@"icon_h":@"feedback_st_h"},
                      @{@"title":@"播放故障",@"icon_n":@"feedback_bfgz_n",@"icon_h":@"feedback_bfgz_h"},
                      @{@"title":@"想看",@"icon_n":@"feedback_xk_n",@"icon_h":@"feedback_xk_h"},
                      @{@"title":@"卡顿",@"icon_n":@"feedback_kd_n",@"icon_h":@"feedback_kd_h"},
                      @{@"title":@"死机",@"icon_n":@"feedback_sj_n",@"icon_h":@"feedback_sj_h"},
                      @{@"title":@"界面错误",@"icon_n":@"feedback_jmcw_n",@"icon_h":@"feedback_jmcw_h"},
                      @{@"title":@"声音故障",@"icon_n":@"feedback_sygz_n",@"icon_h":@"feedback_sygz_h"},
                      @{@"title":@"其他",@"icon_n":@"feedback_qt_n",@"icon_h":@"feedback_qt_h"}];
    
    [self setuptags:tags];
    
    // Do any additional setup after loading the view.
}

- (void)setupNavBar{
    self.navigationItem.title = @"问题反馈";
    
    UIBarButtonItem* saveItem = [[UIBarButtonItem alloc] initWithTitle:@"提交" style:(UIBarButtonItemStylePlain) target:self action:@selector(sumbit)];
    self.navigationItem.rightBarButtonItem = saveItem;
}

- (void)initSubViews{
    
    MyRelativeLayout* titleLayout = [MyRelativeLayout new];
    titleLayout.myHorzMargin = 20;
    titleLayout.myTop = 20;
    titleLayout.myHeight = PX(40);
    titleLayout.myHeight = MyLayoutSize.wrap;
    titleLayout.padding = UIEdgeInsetsMake(10, 16, 10, 16);
    titleLayout.backgroundColor = COLOR_ORANGE_LIGHT;
    [self.contentLayout addSubview:titleLayout];
    
    UILabel* lb1 = [UILabel lableWithFont:FONT_FIT(14) color:COLOR_RED text:@"请选择您在使用过程中，遇到的问题"];
    lb1.textAlignment = NSTextAlignmentCenter;
    lb1.myMargin = 0;
    lb1.myHeight = MyLayoutSize.wrap;
    [titleLayout addSubview:lb1];
    
    // 流式布局
    MyFlowLayout* flowLayout = [MyFlowLayout flowLayoutWithOrientation:MyOrientation_Vert arrangedCount:0];
    flowLayout.myHeight = MyLayoutSize.wrap;
    flowLayout.subviewSpace = 16;
    flowLayout.padding = UIEdgeInsetsMake(16, 16,16, 16);
    flowLayout.myHorzMargin = 20;
    flowLayout.myTop = 20;
    [flowLayout setSubviewsSize:PX(60) minSpace:8 maxSpace:CGFLOAT_MAX centered:NO];
    [self.contentLayout addSubview:flowLayout];
    _flowLayout = flowLayout;
    
    
    UILabel* lb2 = [UILabel lableWithFont:FONTBOLD_FIT(16) color:COLOR_BLACK_DARK text:@"问题/意见描述"];
    lb2.myLeft = 20;
    lb2.myTop = 20;
    [self.contentLayout addSubview:lb2];
    
    MyRelativeLayout* subLayout = [MyRelativeLayout new];
    subLayout.myHorzMargin = 20;
    subLayout.myTop = 8;
    subLayout.myHeight = MyLayoutSize.wrap;
    subLayout.layer.cornerRadius = PX(2);
    subLayout.layer.masksToBounds = YES;
    subLayout.layer.borderColor = COLOR_GRAY_LINE.CGColor;
    subLayout.layer.borderWidth = Size_1PX;
    [self.contentLayout addSubview:subLayout];
    
    UITextView* textView = [UITextView new];
    textView.backgroundColor = UIColor.whiteColor;
    textView.font = FONT_FIT(14);
    textView.textColor = COLOR_BLACK_DARK;
    textView.delegate = self;
    textView.scrollEnabled = NO;
    textView.returnKeyType = UIReturnKeyDone;
    textView.showsHorizontalScrollIndicator = NO;
    textView.showsVerticalScrollIndicator = NO;
    textView.tintColor = COLOR_THEME_A;
    textView.myMargin = 0;
    textView.myHeight = PX(100);
    [subLayout addSubview:textView];
    _textView = textView;
    
    UILabel* placeLb = [UILabel lableWithFont:FONT_FIT(14) color:COLOR_GRAY_QUITE text:@"请填写10字以上的问题描述，以便我们更好的帮您解决问题"];
    placeLb.topPos.equalTo(textView.topPos).offset(4);
    placeLb.leftPos.equalTo(textView.leftPos).offset(4);
    placeLb.rightPos.equalTo(textView.rightPos).offset(4);
    placeLb.myHeight = MyLayoutSize.wrap;
    [subLayout addSubview:placeLb];
    _placeLb = placeLb;
    
    UILabel* numLb = [UILabel lableWithFont:FONT_FIT(12) color:COLOR_GRAY_QUITE text:@"0/100"];
    numLb.myRight = 0;
    numLb.topPos.equalTo(textView.bottomPos).offset(4);
    [subLayout addSubview:numLb];
    _numLb = numLb;
    

    UILabel* lb3 = [UILabel lableWithFont:FONTBOLD_FIT(16) color:COLOR_BLACK_DARK text:@"联系方式"];
    lb3.myHeight = MyLayoutSize.wrap;
    lb3.myHorzMargin = 20;
    lb3.myTop = 20;
    [self.contentLayout addSubview:lb3];
    
    
    XTextField* contactTextField = [[XTextField alloc] init];
    contactTextField.padding = UIEdgeInsetsMake(0, 0, 0, 0);
    contactTextField.myHorzMargin = 20;
    contactTextField.myTop = 0;
    contactTextField.myHeight = PX(44);
    contactTextField.textField.placeholder = @"请填写手机或QQ号,便于我们联系您";
    contactTextField.textField.tintColor = COLOR_THEME;
    [self.contentLayout addSubview:contactTextField];
    _contactTextField = contactTextField;
}

- (void)setuptags:(NSArray*)tags{

    for (int i = 0; i < tags.count; i++) {
        NSDictionary* tag = tags[i];
        
        RSVerButton *tagButton = [RSVerButton buttonWithType:(UIButtonTypeCustom)];

        [tagButton setBackgroundImage:[UIImage imageNamed:tag[@"icon_n"]] forState:UIControlStateNormal];
        [tagButton setBackgroundImage:[UIImage imageNamed:tag[@"icon_h"]] forState:UIControlStateSelected];
        tagButton.backgroundColor = COLOR_BACKGEOUND;
        tagButton.layer.cornerRadius = PX(5);
        tagButton.mySize = CGSizeMake(PX(60), PX(60));
        tagButton.tag = 100+i;
        [tagButton sizeToFit];
        [tagButton addTarget:self action:@selector(tagClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.flowLayout addSubview:tagButton];
    }
}

#pragma mark  -- action
- (void)sumbit{
    
    NSString* text = [self.textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    if(text.length < 10){
        [Toast toast:@"你输入的内容太少了"];
        return;
    }
    
    RFeedbackRequest* request = [RFeedbackRequest new];
    request.advice = text;
    request.action = @"n";
    [RequestOperation encryptedRequest:request quite:YES callBack:NULL];
    
    [self closeViewControllerAnimated:YES];
    [Toast toast:@"反馈成功"];
}

- (void)tagClick:(UIButton*)sender{
    
    for (UIButton* btn in self.flowLayout.subviews) {
        if(btn == sender){
            btn.selected = YES;
        }else{
            btn.selected = NO;
        }
    }
}

#pragma mark  -- UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView{
    
    self.placeLb.visibility = textView.text.length?MyVisibility_Gone:MyVisibility_Visible;
    [self.placeLb sizeToFit];
    
    if(textView.markedTextRange != NULL){
        return;
    }
    
    NSString* text = [textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    if(text.length > 100){
        text = [text substringToIndex:100];
        textView.text = text;
    }
   
    self.numLb.text = [NSString stringWithFormat:@"%ld/100",text.length];
    [self.numLb sizeToFit];
}

@end
